<?php
namespace application\Controller;

use application\Components\BaseController;
use application\Comm\Request;
use application\Module\Designer;
use application\Module\House;
use application\Module\HouseTag;
use library\Core\Config;
use application\Comm\Func;
use application\Module\Adimage;
use application\Module\Adtext;
use library\Core\Db\CDbCriteria;
use application\Module\HouseProductRelat;

class HouseController extends BaseController
{
    /**
     * @throws \application\Components\CException
     * 列表
     */
    public function actionIndex()
    {
    	
        $houseList = new House();
        $houses = [];
        $acreage_id = Request::getParam('acreage_id');//面积
        $type_id = Request::getParam('type_id');//几房几厅
        $style_id = Request::getParam('style_id');//风格
        $ordertype = Request::getParam('ordertype') ? Request::getParam('ordertype') : 'desc';
        $orderby = Request::getParam('orderby') ? Request::getParam('orderby') : 1;
        $status = Config::getParamsVal('STATUS');
        $category = Config::getParamsVal('PRODUCT_CATEGORY');
        $acreage = Config::getParamsVal('HOUSE_ACREAGE');
        $style = Config::getParamsVal('STYLE');
        $type = Config::getParamsVal('HOUSE_TYPE');
        //查找设计师
        $DesignerModule = new Designer();
        $designer = $DesignerModule->getShow();
        $attribute = $houseList->attribute();
		
		$select= [
			'id',
			'name',
			'type_id',
			'designer_id',
			'description',
			'src',
			'unit_src',
			'url',
			'price_all',
			'bdaddr',
			'acreage',
		];
		
        $housesmodule = new House();
        $houseList = $housesmodule->getList($select,$type_id,$style_id,$acreage_id);
        $house_product_relat = new HouseProductRelat();
        $select = [
        		'id',
        		'product_id',
        		'house_id',
        		'space_id',
        		'src'
        ];
        $houses = [];
        foreach ($houseList['data'] as $k=>$v){
        	$houses[$k]['id'] = $v['id'];//house_id
        	$houses[$k]['name'] = $v['name'];//户型名称
        	$houses[$k]['type_id'] = $type[$v['type_id']];//户型id
        	$houses[$k]['description'] = $v['description'];//设计解读
        	$houses[$k]['unit_src'] = Func::imgUrl($v['unit_src']);//户型图
        	$houses[$k]['price_all'] = $v['price_all'];
        	$houses[$k]['bdaddr'] = $v['bdaddr'];
        	$houses[$k]['acreage'] = isset($acreage[$v['acreage']])?$acreage[$v['acreage']]:0;
        	$houses[$k]['src'] =   $housedata = $house_product_relat->Show($v['id']);
        	foreach ($houses[$k]['src'] as $key=>$val){
        		$houses[$k]['src'][$key]['space_id'] = $category[$val['space_id']];
        		if ($houses[$k]['src'][$key]['src'] != ''){
        			$houses[$k]['src'][$key]['src'] =  Func::imgUrl($val['src']);
        		}
        	}
        }
        if ($style_id ==''){
        	$style_id =0;
        }
        if ($acreage_id ==''){
        	$acreage_id = 0;
        }
        if ($type_id ==''){
        	$type_id = 0;
        }
        $this->render('house/index', [
        	'acreage_id'=>$acreage_id,
        	'acreage' =>$acreage,
        	'style_id'=>$style_id,
        	'style' =>$style,
            'houses' => $houses,
            'attribute' => $attribute,
            'type' => $type,
            'type_id' => $type_id,
            'designer' => $designer,
            'orderby' => $orderby,
            'ordertype' => $ordertype,
            'page' => $houseList['page'],
        ]);
    }

    /**
     * @param $id
     * 详情
     * */
    public function actionView($id)
    {
    	$house_id = Request::getParam('house_id');//房屋id
    	$product_id = Request::getParam('product_id');//材料id
    	$space = Config::getParamsVal('HOUSE_SPACE');
    
    	//获取新增的设计解读内容
    	$house = new House();
    	$name = $house->getInfo($house_id,['name','type_id','unit_src','description']);
    	$name['type_id'] = Config::getParamsVal('HOUSE_TYPE',$name['type_id']);
    	
    	$select = [
    			'id',
    			'product_id',
    			'space_id',
    			'view_src',
    			];
		$houseproductrelatmodel = new HouseProductRelat();
		$data = $houseproductrelatmodel->Show($house_id,$select);
		$houses = [];
		foreach ($data as $key =>$val){
			$houses[$key]['id'] = $val['id'];
			$houses[$key]['product_id'] = Func::createUrl('product/view',['id'=>$val['product_id']]);
			$houses[$key]['view_src'] = $val['view_src'];
			$houses[$key]['space_id'] = isset($space[$val['space_id']])?$space[$val['space_id']]:'';
		}
 		
        $this->render('house/view', [
             'houses' => $houses,
        	'name'=>$name ? $name : ['name' => '','unit_src' => '','description' => ''],
        ]);
    }
     

}
